//
//  SecondViewController.m
//  Lesson8HomeWork
//
//  Created by 周琦 on 15/9/26.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "SecondViewController.h"

#define WIDTH (self.view.frame.size.width - 40)
#define HEIGHT (self.view.frame.size.height - 60 - 49)

@interface SecondViewController ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scrollerView;
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, retain) UIImageView *imageView;

@end

@implementation SecondViewController

- (void)dealloc
{
    [_scrollerView release];
    [_pageControl release];
    [_imageView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    self.navigationItem.title = @"第二页";
    [self createScrollerView];
}
#pragma mark - 创建ScrollerView
- (void)createScrollerView
{
    // 创建scrollerView
    self.scrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 30, WIDTH , HEIGHT)];
    // 创建image
    for (int i = 0; i < 8; i++) {
        // 创建图片视图
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
        // 图片名
        NSString *imageName = [NSString stringWithFormat:@"h%d.jpeg", i + 1];
        // 图片
        UIImage *image = [UIImage imageNamed:imageName];
        _imageView.image = image;
        // 加到父视图上
        [_scrollerView addSubview:_imageView];
        [_imageView release];
    }
    // 偏移范围
    _scrollerView.contentSize = CGSizeMake(7* WIDTH, 0);
    
    
    _scrollerView.bounces = NO;
    _scrollerView.pagingEnabled = YES;
    _scrollerView.delegate = self;
    [self.view addSubview:_scrollerView];
    [_scrollerView release];
    
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(_scrollerView.frame.origin.x, _scrollerView.frame.origin.y + HEIGHT + 5, WIDTH, 20)];
    _pageControl.numberOfPages = 7;
    _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    [_pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_pageControl];
    [_pageControl release];
    
}

#pragma mark - ScrollerViewDelegate
- (void)pageControlAction:(UIPageControl *)pageControl
{
    [_scrollerView setContentOffset:CGPointMake(_pageControl.currentPage * WIDTH, 0) animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _pageControl.currentPage = scrollView.contentOffset.x / WIDTH;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
